package com.amigo.online.provider.compare.quartzjob.full;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.amigo.online.provider.common.entity.VideoMongoInfo;
import com.amigo.online.provider.common.entity.VideoMysqlInfo;
import com.amigo.online.provider.common.entity.VideoSolrInfo;
import com.amigo.online.provider.compare.mapper.VideoComparisonMapper;
import com.amigo.online.provider.compare.quartzjob.AbstractVideoComparisonJob;
import com.amigo.online.provider.compare.service.VideoComparisonService;
import com.amigo.online.provider.compare.task.MongoVideoTask;
import com.amigo.online.provider.compare.task.MysqlVideoTask;
import com.amigo.online.provider.compare.task.SolrVideoTask;

public class VideoInfoComparisonJob extends AbstractVideoComparisonJob implements Job {

    @Autowired
    ExecutorService executorService;

    @Autowired
    VideoComparisonMapper videoComparisonMapper;

    @Autowired
    VideoComparisonService videoComparisonService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        List<VideoMongoInfo> videoMongoList = new ArrayList<>();
        List<VideoSolrInfo> videoSolrList = new ArrayList<>();
        List<VideoMysqlInfo> videoMysqlList = new ArrayList<>();
        context.getJobDetail();
        // mysql 和mongo 数据比对
        System.out.println("Mysql-------to----------mongo");

        int maxId = videoComparisonService.getMaxId();
        // 异步操作，把查询的数据塞入到全局变量中
        int index = maxId / 1000 + 1;

        List<FutureTask<List<VideoMongoInfo>>> mongofutureList = new ArrayList<>(maxId);
        List<FutureTask<List<VideoMysqlInfo>>> mysqlfutureList = new ArrayList<>(maxId);
        List<FutureTask<List<VideoSolrInfo>>> solrfutureList = new ArrayList<>(maxId);
        for (int i = 1; i < index; i++) {
            int startId = i + 1000 * (i - 1);
            int endId = startId + 999;
            // 批量查询，并将结果值存在hashMap中
            MongoVideoTask mongoVideoTask = new MongoVideoTask(startId, endId);
            FutureTask<List<VideoMongoInfo>> mongofuture = new FutureTask<>(mongoVideoTask);
            executorService.submit(mongofuture);
            mongofutureList.add(mongofuture);

            MysqlVideoTask mysqlVideoTask = new MysqlVideoTask(startId, endId);
            FutureTask<List<VideoMysqlInfo>> mysqlfuture = new FutureTask<>(mysqlVideoTask);
            executorService.submit(mysqlfuture);
            mysqlfutureList.add(mysqlfuture);

            SolrVideoTask solrVideoTask = new SolrVideoTask(startId, endId);
            FutureTask<List<VideoSolrInfo>> solrfuture = new FutureTask<>(solrVideoTask);
            executorService.submit(solrfuture);
            solrfutureList.add(solrfuture);

        }
        executorService.shutdown();
        try {
            for (FutureTask<List<VideoMongoInfo>> temp : mongofutureList) {
                List<VideoMongoInfo> partVideoMongoList = temp.get();
                videoMongoList.addAll(partVideoMongoList);
            }
            for (FutureTask<List<VideoMysqlInfo>> temp : mysqlfutureList) {
                List<VideoMysqlInfo> partVideoMysqlList = temp.get();
                videoMysqlList.addAll(partVideoMysqlList);
            }
            for (FutureTask<List<VideoSolrInfo>> temp : solrfutureList) {
                List<VideoSolrInfo> partVideoMongoList = temp.get();
                videoSolrList.addAll(partVideoMongoList);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        compareVideo(videoMongoList, videoSolrList, videoMysqlList);
    }


}
